import {
    AddCoupon,
    GetCouponsListWithPage,
    DelCoupon,
    GetCouponById,
    UpdateCoupon
} from '@/services/marketing/coupon';
import {catchException} from '@/utils/utils';
export default {
    namespace: 'marketing',

    state: {
        couponsList: [],
        couponsById: {}
    },

    effects: {
        // 新增优惠券
        *AddCoupon({payload, callBack}, {call, put}) {
            const data = yield call(AddCoupon, payload);
            yield catchException(function* (){
                if(data.Code === 0) {
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
            })            
        },

        // 编辑优惠券
        *UpdateCoupon({payload, callBack}, {call, put}) {
            const data = yield call(UpdateCoupon, payload);
            yield catchException(function* (){
                if(data.Code === 0) {
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
            })            
        },

        // 获取分页
        *GetCouponsListWithPage({payload, callBack}, {call, put}) {
            const data = yield call(GetCouponsListWithPage, payload);
            yield catchException(function* (){
                if(data.Code === 0) {
                    yield put({
                        type: 'saveGetCouponsListWithPage',
                        payload: {
                            dataList: data.Data,
                        },
                    });
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
            })            
        },

        // id获取
        *GetCouponById({payload, callBack}, {call, put}) {
            const data = yield call(GetCouponById, payload);
            yield catchException(function* (){
                if(data.Code === 0) {
                    yield put({
                        type: 'saveGetCouponById',
                        payload: {
                            dataList: data.Data,
                        },
                    });
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
            })            
        },

        //删除
        *DelCoupon({payload, callBack}, {call, put}) {
            const data = yield call(DelCoupon, payload);
            yield catchException(function* (){
                if(data.Code === 0) {
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
            })           
        },

    },

    reducers: {
        saveGetCouponsListWithPage(state, {payload}) {
            return {
                ...state,
                couponsList: payload.dataList,
            };
        },
        saveGetCouponById(state, {payload}) {
            return {
                ...state,
                couponsById: payload.dataList,
            };
        },
 }
}
